package com.infinitus.autocatlog.domain;

import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.xml.bind.DatatypeConverter;

import com.infinitus.autocatlog.service.IpService;
import com.infinitus.autocatlog.service.util.TimeUtil;
import com.infinitus.autocatlog.task.FilterRules;


@Entity
@Table(name="record_item")
public class RecordItem{
	
	public static final String ITEM_TERMINATED = "\001";

	public static final String COLLECTION_TERMINATED = "\002";
	
	public static final String HEAD_TERMINATED = "\\|";

	public static final String MAP_TERMINATED = "\003";

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private long id;
	private String errorType;
	private String osplat;
	
	public String getErrorType() {
		return errorType;
	}




	public void setErrorType(String errorType) {
		this.errorType = errorType;
	}




	public String getOsplat() {
		return osplat;
	}




	public void setOsplat(String osplat) {
		this.osplat = osplat;
	}


	private String sugo_nation;	//国家
	private String sugo_province;	//省份
	private String sugo_city;	//城市
	private String sugo_district;	//地区
	private String sugo_area;	//区域
	private String sugo_latitude;	//纬度
	private String sugo_longitude;	//经度
	private String sugo_city_timezone;	//城市时区
	private String sugo_timezone;	//时区
	private String sugo_phone_code;	//国际区号
	private String sugo_nation_code;	//国家代码
	private String sugo_continent;	//所在大洲
	private String sugo_administrative;	//行政区划代码
	private String sugo_operator;	//运营商
	private String carrier;	//运营商
	private String network;	//网络类型
	private String radio;	//通信协议
	private String sugo_ip;	//客户端IP
	private String sugo_http_forward;	//客户端真实IP
	private String app_name;	//系统名称
	private String app_version;	//系统版本
	private String device_id;	//设备ID
	private String manufacturer;	//设备制造商
	private String device_brand;	//品牌
	private String device_model;	//品牌型号
	private String screen_dpi;	//分辨率
	private String screen_height;	//屏幕高度
	private String screen_width;	//屏幕宽度
	private String bluetooth_version;	//蓝牙版本
	private String has_bluetooth;	//蓝牙功能
	private String has_nfc;	//NFC功能
	private String has_telephone;	//电话功能
	private String has_wifi;	//WIFI功能
	private String google_play_services;	//GooglePlay服务
	private String sugo_user_agent;	//浏览器标识
	private String browser;	//浏览器名称
	private String browser_version;	//浏览器版本
	private String device_agent;	//PC/android/ios
	private String system_name;	//PC/android/ios
	private String device_version;	//操作系统版本
	private String UserID;
	private String distinct_id;	//用户唯一ID
	private String event_id;	//事件ID
	private String event_time;	//客户端事件时间
	private String EventDate;
	private String EventHour;
	private String duration;	//停留时间
	private String page_name;	//页面名称
	private String path_name;	//页面路径
	private String event_type;	//事件类型
	private String event_name;	//事件名称
	private String event_label;	//事件源文本
	private String EventValue;
	private String EventValueType;
	private String Media;
	private String Source;
	private String Creative;
	private String Campaign;
	private String current_url;	//当前请求地址
	private String host;	//客户端域名
	private String sugo_http_refer;	//Referer
	private String referrer;	//客户引荐
	private String referring_domain;	//客户引荐域名
	private String session_id;	//会话ID
	private String sugo_args;	//请求参数
	private String sugo_http_cookie;	//HttpCookie
	private String sugo_lib;	//sdk类型
	private String sdk_version;	//SDK版本
	private String token;	//应用ID
	private String from_binding;	//是否绑定事件
	
	@Column(columnDefinition="text")
	private String Content;
	
	private String UserID4Num;
	
	private String OptName;

	@Column(name="the_month")
	private String YearMonth;

	@Column(name="the_week")
	private String YearWeek;

	private String IsStartup;
	
	private String SceneName;
	
	private String MenuLevel;
	
	private String ColumnName;
	
	private String DayOfWeek;
	
	private String IsNewUser;
	
	private String ChannelFirstLogin;
	
	private String IsFirstLogin;
	
	@Transient
	private Map<String, String> Extras = new LinkedHashMap<String, String>();
	
	private String ignores;
	
	public RecordItem() {
		super();
	}
	
	
	
	
	public void setCrashDateTime(String crashDateTime) {
		Date date = new Date();
		try {
			Date crashDate = TimeUtil.dateTimeFormat.parse(crashDateTime);
			if(date.after(crashDate)){
				date = crashDate;
			}
		} catch (ParseException e) {
			
		}finally{
			event_time = TimeUtil.dateTimeFormat.format(date);
			YearMonth = event_time.substring(0, 7);
			EventDate = event_time.substring(0, 10);
			EventHour = event_time.substring(11, 13);
			DayOfWeek = date.toString().substring(0, 3);
		}
	}
	
	public String getIsFirstLogin() {
		return IsFirstLogin;
	}

	public void setIsFirstLogin(String isFirstLogin) {
		IsFirstLogin = isFirstLogin;
	}

	public String getEventDate() {
		return EventDate;
	}
	
	public String getEventHour() {
		return EventHour;
	}
	
	public void setExtra(String key, String value) {
		Extras.put(key, value);
	}
	
	public Map<String, String> getExtras() {
		return Extras;
	}

//	public String getIsValidData() {
//		if(IsValidData == null)
//			return "Y";
//		return IsValidData;
//	}
//	
	public String getIgnores() {
		if(Extras!=null){
			return Extras.toString();
		}
		return null;
	}


	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}


	public String getSugo_ip() {
		return sugo_ip;
	}

	public void setSugo_ip(String sugo_ip) {
		this.sugo_ip = sugo_ip;
		Map<String, String> ipInfo = IpService.getInstance().findIpInfo(sugo_ip);
		this.sugo_nation = ipInfo.get("Nation");
		this.sugo_province = ipInfo.get("Province");
		this.sugo_city = ipInfo.get("City");
		this.sugo_operator = ipInfo.get("Operator");
	}

	
	public String getSugo_nation() {
		return sugo_nation;
	}


	public String getSugo_province() {
		return sugo_province;
	}


	public String getSugo_city() {
		return sugo_city;
	}


	public String getSugo_district() {
		return sugo_district;
	}

	public void setSugo_district(String sugo_district) {
		this.sugo_district = sugo_district;
	}

	public String getSugo_area() {
		return sugo_area;
	}

	public void setSugo_area(String sugo_area) {
		this.sugo_area = sugo_area;
	}

	public String getSugo_latitude() {
		return sugo_latitude;
	}

	public void setSugo_latitude(String sugo_latitude) {
		this.sugo_latitude = sugo_latitude;
	}

	public String getSugo_longitude() {
		return sugo_longitude;
	}

	public void setSugo_longitude(String sugo_longitude) {
		this.sugo_longitude = sugo_longitude;
	}

	public String getSugo_city_timezone() {
		return sugo_city_timezone;
	}

	public void setSugo_city_timezone(String sugo_city_timezone) {
		this.sugo_city_timezone = sugo_city_timezone;
	}

	public String getSugo_timezone() {
		return sugo_timezone;
	}

	public void setSugo_timezone(String sugo_timezone) {
		this.sugo_timezone = sugo_timezone;
	}

	public String getSugo_phone_code() {
		return sugo_phone_code;
	}

	public void setSugo_phone_code(String sugo_phone_code) {
		this.sugo_phone_code = sugo_phone_code;
	}

	public String getSugo_nation_code() {
		return sugo_nation_code;
	}

	public void setSugo_nation_code(String sugo_nation_code) {
		this.sugo_nation_code = sugo_nation_code;
	}

	public String getSugo_continent() {
		return sugo_continent;
	}

	public void setSugo_continent(String sugo_continent) {
		this.sugo_continent = sugo_continent;
	}

	public String getSugo_administrative() {
		return sugo_administrative;
	}

	public void setSugo_administrative(String sugo_administrative) {
		this.sugo_administrative = sugo_administrative;
	}

	public String getSugo_operator() {
		return sugo_operator;
	}

	public void setSugo_operator(String sugo_operator) {
		this.sugo_operator = sugo_operator;
	}

	public String getCarrier() {
		return carrier;
	}

	public void setCarrier(String carrier) {
		this.carrier = carrier;
	}

	public String getNetwork() {
		return network;
	}

	public void setNetwork(String network) {
		this.network = network;
	}

	public String getRadio() {
		return radio;
	}

	public void setRadio(String radio) {
		this.radio = radio;
	}

	public String getSugo_http_forward() {
		return sugo_http_forward;
	}

	public void setSugo_http_forward(String sugo_http_forward) {
		this.sugo_http_forward = sugo_http_forward;
	}

	public String getApp_name() {
		return app_name;
	}

	public void setApp_name(String app_name) {
		this.app_name = app_name;
	}

	public String getApp_version() {
		return app_version;
	}

	public void setApp_version(String app_version) {
		this.app_version = app_version;
	}

	public String getDevice_id() {
		return device_id;
	}

	public void setDevice_id(String device_id) {
		this.device_id = device_id;
	}

	public String getManufacturer() {
		return manufacturer;
	}

	public void setManufacturer(String manufacturer) {
		this.manufacturer = manufacturer;
	}

	public String getDevice_brand() {
		return device_brand;
	}

	public void setDevice_brand(String device_brand) {
		this.device_brand = toUpperCase(device_brand);
	}

	public String getDevice_model() {
		return device_model;
	}

	public void setDevice_model(String device_model) {
		this.device_model = toUpperCase(device_model);
	}

	public String getScreen_dpi() {
		return screen_dpi;
	}

	public void setScreen_dpi(String screen_dpi) {
		this.screen_dpi = screen_dpi;
	}

	public String getScreen_height() {
		return screen_height;
	}

	public void setScreen_height(String screen_height) {
		this.screen_height = screen_height;
	}

	public String getScreen_width() {
		return screen_width;
	}

	public void setScreen_width(String screen_width) {
		this.screen_width = screen_width;
	}

	public String getBluetooth_version() {
		return bluetooth_version;
	}

	public void setBluetooth_version(String bluetooth_version) {
		this.bluetooth_version = bluetooth_version;
	}

	public String getHas_bluetooth() {
		return has_bluetooth;
	}

	public void setHas_bluetooth(String has_bluetooth) {
		this.has_bluetooth = has_bluetooth;
	}

	public String getHas_nfc() {
		return has_nfc;
	}

	public void setHas_nfc(String has_nfc) {
		this.has_nfc = has_nfc;
	}

	public String getHas_telephone() {
		return has_telephone;
	}

	public void setHas_telephone(String has_telephone) {
		this.has_telephone = has_telephone;
	}

	public String getHas_wifi() {
		return has_wifi;
	}

	public void setHas_wifi(String has_wifi) {
		this.has_wifi = has_wifi;
	}

	public String getGoogle_play_services() {
		return google_play_services;
	}

	public void setGoogle_play_services(String google_play_services) {
		this.google_play_services = google_play_services;
	}

	public String getSugo_user_agent() {
		return sugo_user_agent;
	}

	public void setSugo_user_agent(String sugo_user_agent) {
		this.sugo_user_agent = sugo_user_agent;
	}


	public String getBrowser() {
		return browser;
	}


	public void setBrowser(String browser) {
		this.browser = browser;
	}




	public String getBrowser_version() {
		return browser_version;
	}




	public void setBrowser_version(String browser_version) {
		this.browser_version = browser_version;
	}



	public String getSystem_name() {
		return system_name;
	}




	public void setSystem_name(String system_name) {
		this.system_name = system_name;
	}




	public String getDevice_agent() {
		return device_agent;
	}

	public void setDevice_agent(String device_agent) {
		this.device_agent = device_agent;
	}

	public String getDevice_version() {
		return device_version;
	}

	public void setDevice_version(String device_version) {
		this.device_version = device_version;
	}

	public String getUserID() {
		return UserID;
	}

	public void setUserID(String userID) {
		UserID = userID;
	}

	public String getDistinct_id() {
		return distinct_id;
	}

	public void setDistinct_id(String distinct_id) {
		this.distinct_id = distinct_id;
	}

	public String getEvent_id() {
		return event_id;
	}

	public void setEvent_id(String event_id) {
		this.event_id = event_id;
	}

	public String getEvent_time() {
		return event_time;
	}

	public void setEvent_time(String event_time) {
		this.event_time = event_time;
		Date date = new Date(Long.parseLong(event_time));
		Date now = new Date();
		if(date.after(now)){
			date = now;
		}
		event_time = TimeUtil.dateTimeFormat.format(date);
		YearMonth = event_time.substring(0, 7);
		EventDate = event_time.substring(0, 10);
		EventHour = event_time.substring(11, 13);
		DayOfWeek = date.toString().substring(0, 3);
	}

	public String getDuration() {
		return duration;
	}

	public void setDuration(String duration) {
		this.duration = duration;
	}

	public String getPage_name() {
		return page_name;
	}

	public void setPage_name(String page_name) {
		this.page_name = page_name;
	}

	public String getPath_name() {
		return path_name;
	}

	public void setPath_name(String path_name) {
		this.path_name = path_name;
	}

	public String getEvent_type() {
		return event_type;
	}

	public void setEvent_type(String event_type) {
		this.event_type = event_type;
	}

	public String getEvent_name() {
		return event_name;
	}

	public void setEvent_name(String event_name) {
		this.event_name = event_name;
	}

	public String getEvent_label() {
		return event_label;
	}

	public void setEvent_label(String event_label) {
		this.event_label = event_label;
	}

	public String getEventValue() {
		return EventValue;
	}

	public void setEventValue(String eventValue) {
		EventValue = eventValue;
	}

	public String getEventValueType() {
		return EventValueType;
	}

	public void setEventValueType(String eventValueType) {
		EventValueType = eventValueType;
	}

	public String getMedia() {
		return Media;
	}

	public void setMedia(String media) {
		Media = media;
	}

	public String getSource() {
		return Source;
	}

	public void setSource(String source) {
		Source = source;
	}

	public String getCreative() {
		return Creative;
	}

	public void setCreative(String creative) {
		Creative = creative;
	}

	public String getCampaign() {
		return Campaign;
	}

	public void setCampaign(String campaign) {
		Campaign = campaign;
	}

	public String getCurrent_url() {
		return current_url;
	}

	public void setCurrent_url(String current_url) {
		this.current_url = current_url;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getSugo_http_refer() {
		return sugo_http_refer;
	}

	public void setSugo_http_refer(String sugo_http_refer) {
		this.sugo_http_refer = sugo_http_refer;
	}

	public String getReferrer() {
		return referrer;
	}

	public void setReferrer(String referrer) {
		this.referrer = referrer;
	}

	public String getReferring_domain() {
		return referring_domain;
	}

	public void setReferring_domain(String referring_domain) {
		this.referring_domain = referring_domain;
	}

	public String getSession_id() {
		return session_id;
	}

	public void setSession_id(String session_id) {
		this.session_id = session_id;
	}

	public String getSugo_args() {
		return sugo_args;
	}

	public void setSugo_args(String sugo_args) {
		this.sugo_args = sugo_args;
	}

	public String getSugo_http_cookie() {
		return sugo_http_cookie;
	}

	public void setSugo_http_cookie(String sugo_http_cookie) {
		this.sugo_http_cookie = sugo_http_cookie;
	}

	public String getSugo_lib() {
		return sugo_lib;
	}

	public void setSugo_lib(String sugo_lib) {
		this.sugo_lib = sugo_lib;
	}

	public String getSdk_version() {
		return sdk_version;
	}

	public void setSdk_version(String sdk_version) {
		this.sdk_version = sdk_version;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public String getFrom_binding() {
		return from_binding;
	}

	public void setFrom_binding(String from_binding) {
		this.from_binding = from_binding;
	}

	public String getContent() {
		return Content;
	}

	public void setContent(String content) {
		Content = content;
	}

	public String getUserID4Num() {
		return UserID4Num;
	}

	public void setUserID4Num(String userID4Num) {
		UserID4Num = userID4Num;
	}

	public String getOptName() {
		return OptName;
	}

	public void setOptName(String optName) {
		OptName = optName;
	}

	public String getYearMonth() {
		return YearMonth;
	}

	public String getYearWeek() {
		return YearWeek;
	}

	public void setYearWeek(String yearWeek) {
		YearWeek = yearWeek;
	}

	public String getIsStartup() {
		return IsStartup;
	}

	public void setIsStartup(String isStartup) {
		IsStartup = isStartup;
	}

	public String getSceneName() {
		return SceneName;
	}

	public void setSceneName(String sceneName) {
		SceneName = sceneName;
	}

	public String getMenuLevel() {
		return MenuLevel;
	}

	public void setMenuLevel(String menuLevel) {
		MenuLevel = menuLevel;
	}

	public String getColumnName() {
		return ColumnName;
	}

	public void setColumnName(String columnName) {
		ColumnName = columnName;
	}

	public String getDayOfWeek() {
		return DayOfWeek;
	}

	public String getIsNewUser() {
		return IsNewUser;
	}

	public void setIsNewUser(String isNewUser) {
		IsNewUser = isNewUser;
	}


	public String getChannelFirstLogin() {
		return ChannelFirstLogin;
	}

	public void setChannelFirstLogin(String channelFirstLogin) {
		ChannelFirstLogin = channelFirstLogin;
	}

	public void setIgnores(String ignores) {
		this.ignores = ignores;
	}

	private String toUpperCase(String str){
		if(str==null || "".equals(str.trim())){
			return "";
		}
		return str.toUpperCase();
	}
	
	
	private static String decode(String key) {
		try {
			return URLDecoder.decode(key, "utf-8");
		} catch (Exception e) {
			return key;
		}
	}
	
	private static String[] concat(String[] a, String[] b) {
		String[] c = new String[a.length + b.length];
		System.arraycopy(a, 0, c, 0, a.length);
		System.arraycopy(b, 0, c, a.length, b.length);
		return c;
	}

	public static List<RecordItem> converts(String value, Map<String,String> fieldTypes, String topic) {
		List<RecordItem> items = new ArrayList<RecordItem>();
		try{
			String[] title = value.split("\t");
			String ip = title[4];
			System.out.println(ip);
			String str = new String (DatatypeConverter.parseBase64Binary(title[title.length-1]));
			System.out.println(str);
			
			String[] table = str.split(COLLECTION_TERMINATED);
			if(table.length>1){
				String[] ths = table[0].split(",");
				for(int i =1; i<table.length; i++){
					RecordItem item = new RecordItem();
					String[] tds = table[i].split(ITEM_TERMINATED);
					if(tds.length<ths.length){
						tds = concat(tds, new String[ths.length-tds.length]);
						System.out.println("head长："+ths.length+"===========================body长："+tds.length);
					}
					for (int j = 0; j < ths.length; j++) {
						String th = ths[j].split(HEAD_TERMINATED)[1];
						fieldTypes.put(th, ths[j].split(HEAD_TERMINATED)[0]);
						try {
							Method method = RecordItem.class.getDeclaredMethod("set" + th.substring(0, 1).toUpperCase()+th.substring(1), String.class);
							method.invoke(item, tds[j]);
						} catch (NoSuchMethodException e) {
							item.setExtra(th, tds[j]);
						} catch (Exception e) {
							item.setExtra(th, tds[j]);
						}
					}
					item.setSugo_ip(ip);
					items.add(item);
				}
				
			}
		}catch(Exception e){
			e.printStackTrace();
			return items;
		}
		return items;
	}
	
	public static RecordItem convert(String record) {
		RecordItem item = new RecordItem();
		String[] tabs = record.split("&");

		for (String tab : tabs) {
			String[] kv = tab.split("=");

			if (kv.length == 2) {
				String key = decode(kv[0]);
				String value = decode(kv[1]);
				
				try {
					Method method = RecordItem.class.getDeclaredMethod("set" + key, String.class);

					method.invoke(item, value);
				} catch (NoSuchMethodException e) {
					item.setExtra(key, value);
				} catch (Exception e) {
					item.setExtra(key, value);
				}
			}
		}
		return item;
	}
	

	public String toHiveTest() {
		StringBuilder sb = new StringBuilder(256);

		sb.append("IP").append(ITEM_TERMINATED);
		sb.append("Nation").append(ITEM_TERMINATED);
		sb.append("Province").append(ITEM_TERMINATED);
		sb.append("City").append(ITEM_TERMINATED);
		sb.append("Operator").append(ITEM_TERMINATED);
		sb.append("Network").append(ITEM_TERMINATED);
		sb.append("SystemName").append(ITEM_TERMINATED);
		sb.append("SystemVersion").append(ITEM_TERMINATED);
		sb.append("UserID").append(ITEM_TERMINATED);
		sb.append("SessionID").append(ITEM_TERMINATED);
		sb.append("ClientDeviceID").append(ITEM_TERMINATED);
		sb.append("ClientDeviceBrand").append(ITEM_TERMINATED);
		sb.append("ClientDeviceModel").append(ITEM_TERMINATED);
		sb.append("ClientDeviceAgent").append(ITEM_TERMINATED);
		sb.append("ClientDeviceVersion").append(ITEM_TERMINATED);
		sb.append("OsScreen").append(ITEM_TERMINATED);
		sb.append("EventDateTime").append(ITEM_TERMINATED);
		sb.append("EventDate").append(ITEM_TERMINATED);
		sb.append("EventHour").append(ITEM_TERMINATED);
		sb.append("EventScreen").append(ITEM_TERMINATED);
		sb.append("EventAction").append(ITEM_TERMINATED);
		sb.append("EventLabel").append(ITEM_TERMINATED);
		sb.append("EventValue").append(ITEM_TERMINATED);
		sb.append("Referrer").append(ITEM_TERMINATED);
		sb.append("Campaign").append(ITEM_TERMINATED);
		sb.append("Source").append(ITEM_TERMINATED);
		sb.append("Media").append(ITEM_TERMINATED);
		sb.append("Creative").append(ITEM_TERMINATED);
		//
		sb.append("OptName").append(ITEM_TERMINATED);
		sb.append("YearMonth").append(ITEM_TERMINATED);
		sb.append("YearWeek").append(ITEM_TERMINATED);
		sb.append("IsValidData").append(ITEM_TERMINATED);
		
		int size = 3;
		int index = 0;

		for (int i = 0; i < 3; i++) {
			sb.append("key" + i).append(MAP_TERMINATED).append("value" + i);
			index++;
			if (index < size) {
				sb.append(COLLECTION_TERMINATED);
			} else {
				sb.append(ITEM_TERMINATED);
			}
		}
		return sb.toString();
	}

	
	public String toHive() {
		StringBuilder sb = new StringBuilder(400);

		sb.append(sugo_nation).append(ITEM_TERMINATED);
		sb.append(sugo_province).append(ITEM_TERMINATED);
		sb.append(sugo_city).append(ITEM_TERMINATED);
		sb.append(sugo_district).append(ITEM_TERMINATED);
		sb.append(sugo_area).append(ITEM_TERMINATED);
		sb.append(sugo_latitude).append(ITEM_TERMINATED);
		sb.append(sugo_longitude).append(ITEM_TERMINATED);
		sb.append(sugo_city_timezone).append(ITEM_TERMINATED);
		sb.append(sugo_timezone).append(ITEM_TERMINATED);
		sb.append(sugo_phone_code).append(ITEM_TERMINATED);
		sb.append(sugo_nation_code).append(ITEM_TERMINATED);
		sb.append(sugo_continent).append(ITEM_TERMINATED);
		sb.append(sugo_administrative).append(ITEM_TERMINATED);
		sb.append(sugo_operator).append(ITEM_TERMINATED);
		sb.append(carrier).append(ITEM_TERMINATED);
		sb.append(network).append(ITEM_TERMINATED);
		sb.append(radio).append(ITEM_TERMINATED);
		sb.append(sugo_ip).append(ITEM_TERMINATED);
		sb.append(sugo_http_forward).append(ITEM_TERMINATED);
		sb.append(app_name).append(ITEM_TERMINATED);
		sb.append(app_version).append(ITEM_TERMINATED);
		sb.append(device_id).append(ITEM_TERMINATED);
		sb.append(manufacturer).append(ITEM_TERMINATED);
		sb.append(device_brand).append(ITEM_TERMINATED);
		sb.append(device_model).append(ITEM_TERMINATED);
		sb.append(screen_dpi).append(ITEM_TERMINATED);
		sb.append(screen_height).append(ITEM_TERMINATED);
		sb.append(screen_width).append(ITEM_TERMINATED);
		sb.append(bluetooth_version).append(ITEM_TERMINATED);
		sb.append(has_bluetooth).append(ITEM_TERMINATED);
		sb.append(has_nfc).append(ITEM_TERMINATED);
		sb.append(has_telephone).append(ITEM_TERMINATED);
		sb.append(has_wifi).append(ITEM_TERMINATED);
		sb.append(google_play_services).append(ITEM_TERMINATED);
		sb.append(sugo_user_agent).append(ITEM_TERMINATED);
		sb.append(browser).append(ITEM_TERMINATED);
		sb.append(browser_version).append(ITEM_TERMINATED);
		sb.append(device_agent).append(ITEM_TERMINATED);
		sb.append(device_version).append(ITEM_TERMINATED);
		sb.append(UserID).append(ITEM_TERMINATED);
		sb.append(distinct_id).append(ITEM_TERMINATED);
		sb.append(event_id).append(ITEM_TERMINATED);
		sb.append(event_time).append(ITEM_TERMINATED);
		sb.append(EventDate).append(ITEM_TERMINATED);
		sb.append(EventHour).append(ITEM_TERMINATED);
		sb.append(duration).append(ITEM_TERMINATED);
		sb.append(page_name).append(ITEM_TERMINATED);
		sb.append(path_name).append(ITEM_TERMINATED);
		sb.append(event_type).append(ITEM_TERMINATED);
		sb.append(event_name).append(ITEM_TERMINATED);
		sb.append(event_label).append(ITEM_TERMINATED);
		sb.append(EventValue).append(ITEM_TERMINATED);
		sb.append(EventValueType).append(ITEM_TERMINATED);
		sb.append(Media).append(ITEM_TERMINATED);
		sb.append(Source).append(ITEM_TERMINATED);
		sb.append(Creative).append(ITEM_TERMINATED);
		sb.append(Campaign).append(ITEM_TERMINATED);
		sb.append(current_url).append(ITEM_TERMINATED);
		sb.append(host).append(ITEM_TERMINATED);
		sb.append(sugo_http_refer).append(ITEM_TERMINATED);
		sb.append(referrer).append(ITEM_TERMINATED);
		sb.append(referring_domain).append(ITEM_TERMINATED);
		sb.append(session_id).append(ITEM_TERMINATED);
		sb.append(sugo_args).append(ITEM_TERMINATED);
		sb.append(sugo_http_cookie).append(ITEM_TERMINATED);
		sb.append(sugo_lib).append(ITEM_TERMINATED);
		sb.append(sdk_version).append(ITEM_TERMINATED);
		sb.append(token).append(ITEM_TERMINATED);
		sb.append(from_binding).append(ITEM_TERMINATED);
		
		
		sb.append(Content).append(ITEM_TERMINATED);
		sb.append(UserID4Num).append(ITEM_TERMINATED);
		sb.append(OptName).append(ITEM_TERMINATED);
		sb.append(YearMonth).append(ITEM_TERMINATED);
		sb.append(YearWeek).append(ITEM_TERMINATED);
		sb.append(IsStartup).append(ITEM_TERMINATED);
		sb.append(SceneName).append(ITEM_TERMINATED);
		sb.append(MenuLevel).append(ITEM_TERMINATED);
		sb.append(ColumnName).append(ITEM_TERMINATED);
		sb.append(DayOfWeek).append(ITEM_TERMINATED);
		sb.append(IsNewUser).append(ITEM_TERMINATED);
		
		int size = Extras.size();
		int index = 0;

		for (Entry<String, String> entry : Extras.entrySet()) {
			sb.append(entry.getKey()).append(MAP_TERMINATED).append(entry.getValue());
			index++;
			if (index < size) {
				sb.append(COLLECTION_TERMINATED);
			} else {
				sb.append(ITEM_TERMINATED);
			}
		}
		return sb.toString();
	}
	
	protected String encode(String key) {
		try {
			return key;
//			return URLEncoder.encode(key, "utf-8");
		} catch (Exception e) {
			return key;
		}
	}

	
	@Override
	public String toString() {
		String ke = "&";
		StringBuffer record = new StringBuffer();
		cutField(record, "sugo_nation", sugo_nation);
		cutField(record, "sugo_province", sugo_province);
		cutField(record, "sugo_city", sugo_city);
		cutField(record, "sugo_district", sugo_district);
		cutField(record, "sugo_area", sugo_area);
		cutField(record, "sugo_latitude", sugo_latitude);
		cutField(record, "sugo_longitude", sugo_longitude);
		cutField(record, "sugo_city_timezone", sugo_city_timezone);
		cutField(record, "sugo_timezone", sugo_timezone);
		cutField(record, "sugo_phone_code", sugo_phone_code);
		cutField(record, "sugo_nation_code", sugo_nation_code);
		cutField(record, "sugo_continent", sugo_continent);
		cutField(record, "sugo_administrative", sugo_administrative);
		cutField(record, "sugo_operator", sugo_operator);
		cutField(record, "carrier", carrier);
		cutField(record, "network", network);
		cutField(record, "radio", radio);
		cutField(record, "sugo_ip", sugo_ip);
		cutField(record, "sugo_http_forward", sugo_http_forward);
		cutField(record, "app_name", app_name);
		cutField(record, "app_version", app_version);
		cutField(record, "device_id", device_id);
		cutField(record, "manufacturer", manufacturer);
		cutField(record, "device_brand", device_brand);
		cutField(record, "device_model", device_model);
		cutField(record, "screen_dpi", screen_dpi);
		cutField(record, "screen_height", screen_height);
		cutField(record, "screen_width", screen_width);
		cutField(record, "bluetooth_version", bluetooth_version);
		cutField(record, "has_bluetooth", has_bluetooth);
		cutField(record, "has_nfc", has_nfc);
		cutField(record, "has_telephone", has_telephone);
		cutField(record, "has_wifi", has_wifi);
		cutField(record, "google_play_services", google_play_services);
		cutField(record, "sugo_user_agent", sugo_user_agent);
		cutField(record, "browser", browser);
		cutField(record, "browser_version", browser_version);
		cutField(record, "device_agent", device_agent);
		cutField(record, "device_version", device_version);
		cutField(record, "UserID", UserID);
		cutField(record, "distinct_id", distinct_id);
		cutField(record, "event_id", event_id);
		cutField(record, "event_time", event_time);
		cutField(record, "EventDate", EventDate);
		cutField(record, "EventHour", EventHour);
		cutField(record, "duration", duration);
		cutField(record, "page_name", page_name);
		cutField(record, "path_name", path_name);
		cutField(record, "event_type", event_type);
		cutField(record, "event_name", event_name);
		cutField(record, "event_label", event_label);
		cutField(record, "EventValue", EventValue);
		cutField(record, "EventValueType", EventValueType);
		cutField(record, "Media", Media);
		cutField(record, "Source", Source);
		cutField(record, "Creative", Creative);
		cutField(record, "Campaign", Campaign);
		cutField(record, "current_url", current_url);
		cutField(record, "host", host);
		cutField(record, "sugo_http_refer", sugo_http_refer);
		cutField(record, "referrer", referrer);
		cutField(record, "referring_domain", referring_domain);
		cutField(record, "session_id", session_id);
		cutField(record, "sugo_args", sugo_args);
		cutField(record, "sugo_http_cookie", sugo_http_cookie);
		cutField(record, "sugo_lib", sugo_lib);
		cutField(record, "sdk_version", sdk_version);
		cutField(record, "token", token);
		cutField(record, "from_binding", from_binding);
		
		for (Entry<String, String> entry : Extras.entrySet()) {
			record.append(entry.getKey()+"="+encode(entry.getValue())+ke);
		}
		
		record.append("Content=" + encode(Content)+ ke);
		record.append("UserID4Num=" + encode(UserID4Num)+ ke);
		record.append("OptName=" + encode(OptName)+ ke);
		record.append("YearMonth=" + encode(YearMonth)+ ke);
		record.append("YearWeek=" + encode(YearWeek)+ ke);
		record.append("IsStartup=" + encode(IsStartup)+ ke);
		record.append("SceneName=" + encode(SceneName)+ ke);
		record.append("MenuLevel=" + encode(MenuLevel)+ ke);
		record.append("ColumnName=" + encode(ColumnName)+ ke);
		record.append("DayOfWeek=" + encode(DayOfWeek)+ ke);
		record.append("IsNewUser=" + encode(IsNewUser)+ ke);
		record.append("IsFirstLogin=" + encode(IsFirstLogin)+ ke);
		record.append("ChannelFirstLogin=" + encode(ChannelFirstLogin));
		
		return record.toString();
	}
	
	private void cutField(StringBuffer sb ,String FieldName,String FieldValue){
		if(FilterRules.isNotBlank(FieldValue)){
			sb.append(FieldName+"="+encode(FieldValue)+"&");
		}
	}
	public static void main(String[] args) {
		String str = "iPhonetrueiPhone8,1wifiObjective-Cba4d7f8ba52e68dba209eeeb8bd66e1d375NoneAppleiPhone OS620CFAD5-75BC-4684-BDD2-2A71073140C3095FB755-420E-47BB-A60C-A36D3B3F294714882481875706672.0.0启动启动9.2AE88BBF9-1A76-4FF9-9F46-780EB1A138E72.0.02.0.0";
		String[] arr = new String[5];
		
		for (int j = 0; j < arr.length; j++) {
			System.out.println(arr[j]);
		}
	}

}
